#include<iostream>
#include<stack>
#include<cstring>
#include<cstdlib>
#include<stdio.h>

using namespace std;

typedef struct Node
{
	Node(int h,int idx):height(h),index(idx) 
	{}

	unsigned long height;
	int index;
}tNode;

stack<tNode> s;
unsigned long total = 0;
const unsigned long MAX = 2000000001;

inline void work(unsigned long h,int i)
{
	if( s.empty() || h < s.top().height )
	{
		s.push(tNode(h,i));
	}
	else
	{
		while( !s.empty() && h >= s.top().height )
		{
			total += i-s.top().index-1;
			//cout<<"in while total = "<<total<<" i = "<<i<<" s.top().index = "<<s.top().index<<endl;
			s.pop();
		}//while
		s.push(tNode(h,i));
	}
}

int main()
{
	int n;
	unsigned long h;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		cin>>h;
		work(h,i);	
	}//for
	work(MAX,n+1);
	cout<<total<<endl;
	//system("pause") ;
	return 0;
}
